package cn.tedu.analyse.controller;

import cn.tedu.analyse.dao.mapper.RepliesMapper;
import cn.tedu.analyse.pojo.param.CommentsParam;
import cn.tedu.analyse.pojo.vo.CommentsVO;
import cn.tedu.analyse.pojo.vo.RepliesVO;
import cn.tedu.analyse.response.JsonResult;
import cn.tedu.analyse.service.CommentsService;
import cn.tedu.analyse.service.RepliesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/comments")
@Api(tags = "评论")
public class CommentsController {
    @Autowired
    private CommentsService commentsService;

    @Autowired
    private RepliesService repliesService;

    @Autowired
    private RepliesMapper repliesMapper;

    @ApiOperation("创建评论")
    @PostMapping("create")
    public JsonResult createComment(@RequestBody CommentsParam commentsParam) {
        log.debug("创建评论，参数：{}", commentsParam);
        commentsService.createComment(commentsParam);
        return JsonResult.ok();
    }

    @ApiOperation("获取评论列表")
    @GetMapping("list/{articleId}")
    public JsonResult listComments(@PathVariable Long articleId) {
        log.debug("获取评论列表，参数：{}", articleId);
        List<CommentsVO> list = commentsService.listComments(articleId);
        for (CommentsVO commentsVO : list) {
            List<RepliesVO> replies = repliesService.listReplies(commentsVO.getId());
            commentsVO.setReplies(replies);

            for (RepliesVO repliesVO : replies) {
                repliesMapper.selectReplies(repliesVO.getId());
            }
        }
        return JsonResult.ok(list);
    }


    @ApiOperation("删除评论")
    @DeleteMapping("delete/{commentId}")
    public JsonResult deleteComment(@PathVariable Long commentId) {
        log.debug("删除评论，参数：{}", commentId);
        commentsService.deleteComment(commentId);
        return JsonResult.ok();
    }


}
